package objectOriented

object ApplyApp {
  def main(args: Array[String]): Unit = {
    /*for(i <- 1 to 10){
      ApplyTest.incr()
    }
    println(ApplyTest.count)  // 10 说明object本身是个单例对象*/

    var a: Unit = ApplyTest()   // 伴生类名+ 括号 调用 object.apply()

    var b = new ApplyTest()
    println(b)

    b()                      // 伴生类实例化对象+括号 调用 class.apply()
  }

}

// 伴生类 和 伴生对象
class ApplyTest{
  def apply():Unit = {      // 名字必须是apply
    println("class apply...")
  }
}

object ApplyTest{
  println("object ApplyTest enter ...")
  var count = 0
  def incr():Unit = {
    count += 1
  }


  // 最佳实践 ： Object的apply方法中 new Class
  def apply():ApplyTest = {      // 名字必须是apply
    println("object apply...")

    new ApplyTest
  }

  println("object ApplyTest leave ...")
}